﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.DB;
using MaxLeaf.Common;
using System.Data.SqlClient;
using System.Data;

namespace MaxLeaf.DAL.K3
{
    /*
     exec sp_executesql N'INSERT INTO t_Item (FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted) 
     * VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10)',N'@P1 int,@P2 int,@P3 smallint,@P4 varchar(80),@P5 varchar(80),@P6 varchar(80),@P7 varchar(80),@P8 bit,@P9 varchar(38),@P10 smallint'
     * ,4,9341,2,'test01.02','T1.02','02','T1.02',0,'F1841DD0-CB58-4ADE-B57A-C2D77D1539DA',0
     * 
     * Delete from Access_t_ICItem where FItemID=11191
 Insert into Access_t_ICItem(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
 Values(11191,9341,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
     * 
     * 
     * exec sp_executesql N'UPDATE t_Item SET FNumber=@P1,FName=@P2,FFullNumber=@P3,FShortNumber=@P4 WHERE FItemID=11191'
     * ,N'@P1 varchar(80),@P2 varchar(80),@P3 varchar(80),@P4 varchar(80)','T1.02','t1.02','T1.02','02'
     * */

    //上级组
    public class ImInvGroupDAL : BaseDAL
    {
        
        public ImInvGroupDAL()
            : base()        
        {
            strTableName = "t_Item";
            strFieldNames = @"";

        }
        

        public override void Update(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        {
            DataRow drOrgData = dbOrgCon.GetTableDataFirstRow(strTableName, new Dictionary<string, object> { { "FItemID", strKeyValue } }, "Fnumber");
            string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
            GetFParentID("4", dbOrgCon, dbCon, arrFnumber);
            GetFParentID("2001", dbOrgCon, dbCon, arrFnumber);

        }

          

        private string GetFParentID(string strFItemClassID, DBAccess dbOrgCon, DBAccess dbDestCon, string[] arrFnumber)
        {

            const int intFDetail = 0;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();
            for (int i = 0; i < arrFnumber.Length  ; i++)
            {
                string strFParentID = "0";
                if (i != 0)
                {
                    strFParentID = strFItemId;
                }
                //得到每级的FItemId
                strFItemId = dal.GetFItemId(dbOrgCon, dbDestCon, strFItemClassID, i + 1, string.Empty, strFParentID, string.Join(".", arrFnumber, 0, i + 1), intFDetail);
                if (strFItemClassID.Equals("4"))
                {
                    UpdateAccess_t_ICItem(dbDestCon, strFItemId, strFParentID);
                }
                else
                {
                    UpdateAccess_cbCostObj(dbDestCon, strFItemId, strFParentID);
                }



            }

            return strFItemId;
        }

        private void UpdateAccess_t_ICItem(DBAccess dbDestCon, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from Access_t_ICItem where FItemID=@FItemID)
                begin
                 Insert into Access_t_ICItem(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FItemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))

                end
                ";
            dbDestCon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FItemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }

        private void UpdateAccess_cbCostObj(DBAccess dbDestCon, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from Access_cbCostObj where FItemID=@FItemID)
                begin
                 Insert into Access_cbCostObj(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                Values(@FItemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))

                end
                ";
            dbDestCon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FItemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }
         

        public override string GetDestMapKeyValue(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFItemID)
        {
            if (dbDestCon.ServerMasterDB)
            {
                return strOrgFItemID;
            }

            KeyValuePair<string, IDataParameter[]> kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "TableName", "t_item" }, { "OrgKeyValue", strOrgFItemID } });
            string strSql = string.Format("select DestKeyValue from mkeymap where {0}", kvp.Key);

            string strFitemid = dbDestCon.GetValue(strSql, kvp.Value);
            if (strFitemid.Length > 0)
            {
                return strFitemid;
            }

            Update(dbOrgCon, dbDestCon, strOrgFItemID);

            kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "TableName", "t_item" }, { "OrgKeyValue", strOrgFItemID } });
            return dbDestCon.GetValue(strSql, kvp.Value);
        }

        //public override void Delete(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        //{
        //    throw new MaxLeafException("Delete 上级组 没实现");
        //}
    }
}
